package com.ideal.ete.collector.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ideal.ete.collector.dao.TraceDao;
import com.ideal.ete.collector.server.bo.SpanBo;
import com.ideal.ete.collector.server.bo.SpanBo2;
import com.ideal.ete.collector.service.FilteredMapService;
import com.ideal.ete.collector.utils.TransactionId;
import com.ideal.ete.collector.vo.LimitedScanResult;
import com.ideal.ete.collector.vo.Range;

@Controller
public class ScatterController {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());


	@Autowired
	private FilteredMapService flow;


	// add by Leo
	@Autowired
	private FilteredMapService filteredMapService;

	// add by Leo
	@Autowired
	private TraceDao hbaseTraceDaoV2;

	private static final String PREFIX_TRANSACTION_ID = "I";
	private static final String PREFIX_TIME = "T";
	private static final String PREFIX_RESPONSE_TIME = "R";

	/**
	 * selected points from scatter chart data query
	 *
	 * @param requestParam
	 * @return
	 */
	@RequestMapping(value = "/transactionListForConditions", method = RequestMethod.GET)
	@ResponseBody
	public List<SpanBo> transactionInfo(
			@RequestParam("applicationName") String applicationName,
			@RequestParam(value = "startTimestamp", required = false, defaultValue = "0") long startTimestamp,
			@RequestParam(value = "endTimestamp", required = false, defaultValue = "0") long endTimestamp,
			@RequestParam(value = "limit", required = false, defaultValue = "1000") int limit) {
		Range range = new Range(startTimestamp, endTimestamp);
		LimitedScanResult<List<TransactionId>> limitedScanResult = filteredMapService
				.selectTraceIdsFromApplicationTraceIndex(applicationName,
						range, limit);
		List<TransactionId> tranIdList = limitedScanResult.getScanData();
		List<List<SpanBo>> traceList = hbaseTraceDaoV2.selectSpans(tranIdList);
		List<SpanBo> spanBoList = new ArrayList<>();
		for (List<SpanBo> trace : traceList) {
			for (SpanBo spanBo : trace) {
				if (applicationName.equals(spanBo.getApplicationId())) {
					spanBoList.add(spanBo);
				}
			}
		}
		return spanBoList;
	}
	
	
	
	@RequestMapping(value = "/transactionListForConditions2", method = RequestMethod.GET)
	@ResponseBody
	public List<SpanBo2> transactionInfo2(
			@RequestParam("applicationName") String applicationName,
			@RequestParam(value = "startTimestamp", required = false, defaultValue = "0") long startTimestamp,
			@RequestParam(value = "endTimestamp", required = false, defaultValue = "0") long endTimestamp,
			@RequestParam(value = "limit", required = false, defaultValue = "1000") int limit) {
		System.out.println("~~~~~~"+System.currentTimeMillis());
		Range range = new Range(startTimestamp, endTimestamp);
		LimitedScanResult<List<TransactionId>> limitedScanResult = filteredMapService
				.selectTraceIdsFromApplicationTraceIndex(applicationName,
						range, limit);
		List<TransactionId> tranIdList = limitedScanResult.getScanData();
		System.out.println("-------"+System.currentTimeMillis());
		List<List<SpanBo2>> traceList = hbaseTraceDaoV2.selectSpans2(tranIdList);
		System.out.println("-------"+System.currentTimeMillis());
		List<SpanBo2> spanBoList = new ArrayList<>();
		for (List<SpanBo2> trace : traceList) {
			for (SpanBo2 spanBo2 : trace) {
				if (applicationName.equals(spanBo2.getApplicationId())) {
					spanBoList.add(spanBo2);
				}
			}
		}
		System.out.println("~~~~~~"+System.currentTimeMillis());
		return spanBoList;
	}
	
}
